//战役campaign数据的展开，生成用于游戏加速的缓存数据
//append troopType attributes into troop

var CampaignScriptInitialize = function(tileEngine){
	
	//势力点land_type为0，无法通行
	for (var forceid = 0; forceid < campaign.force.length; forceid++){
		campaign.force[forceid].tileIndex = tileEngine.TileToIndex(campaign.force[forceid].tile);
		campaign.zone.land_type[ campaign.force[forceid].tileIndex ] = 0;
	}
	
	//直接地块通行指数，用于计算距离
	campaign.zone.direct_index = CreateArray(0, campaign.zone.xsize * campaign.zone.ysize, 0.5, false);
	
	//某force的第几个troop
	var forcetroopid = CreateArray(0, campaign.force.length, 0, false);
	
	for (var troopid = 0; troopid < campaign.troop.length; troopid++){
		var tileIndex = tileEngine.TileToIndex(campaign.troop[troopid].tile);
		campaign.troop[troopid].tileIndex = tileIndex;
		
		campaign.troop[troopid].tieTimer = 0;
		//tieTimer每个tick减少1，最低减到 0。
		//每当被击溃时，tieTimer = 5。
		//每当被牵制时，tieTimer = 2。
		//当tieTimer在1~3时，部队处于被牵制状态，无法移动
		//当tieTimer在4~5时，部队处于被击溃状态，无法移动攻击
		// ** 实际参数以 campaign.judgement 中的设置为准
		
		var typeid = campaign.troop[troopid].troop_type;
		
		campaign.troop[troopid].type_name = campaign.troop_type[typeid].name;
		campaign.troop[troopid].base_image = DeepClone(campaign.troop_type[typeid].base_image);
		campaign.troop[troopid].standby_effect = DeepClone(campaign.troop_type[typeid].standby_effect);
		campaign.troop[troopid].court_force = campaign.troop_type[typeid].court_force;
		campaign.troop[troopid].man_strength = campaign.troop_type[typeid].man_strength;
		campaign.troop[troopid].attack_efficiency = DeepClone(campaign.troop_type[typeid].attack_efficiency);
		campaign.troop[troopid].attack_range = campaign.troop_type[typeid].attack_range;
		campaign.troop[troopid].attack_range_decline = campaign.troop_type[typeid].attack_range_decline;
		campaign.troop[troopid].ammo_limit = campaign.troop_type[typeid].ammo_limit;
		campaign.troop[troopid].move = campaign.troop_type[typeid].move;
		campaign.troop[troopid].tickMove = campaign.troop_type[typeid].move / campaign.time_ref.ticks_of_step;
		campaign.troop[troopid].move_ability = DeepClone(campaign.troop_type[typeid].move_ability);
		campaign.troop[troopid].fuel_limit = campaign.troop_type[typeid].fuel_limit;
		campaign.troop[troopid].detect_range = campaign.troop_type[typeid].detect_range;
		campaign.troop[troopid].conceal_range = campaign.troop_type[typeid].conceal_range;
		
		var attackRate = 0;
		var defendRate = 0;
		for (var typeid_tgt = 0; typeid_tgt < campaign.troop_type.length; typeid_tgt++){
			attackRate += campaign.troop_type[typeid].attack_efficiency[typeid_tgt];
			defendRate += campaign.troop_type[typeid_tgt].attack_efficiency[typeid];
		}
		campaign.troop[troopid].attackDefendRate = attackRate / defendRate;
		
		var forceid = campaign.troop[troopid].force;
		var colorAry = campaign.force[forceid].color;
		var color = cc.color(colorAry[0],colorAry[1],colorAry[2],colorAry[3]);
		campaign.troop[troopid].color = colorEngine.GetRandomColor(color, 40, forcetroopid[forceid]);
		forcetroopid[forceid]++;
	}
};

var campaignHelper = {
	GetArroundEnemy : function(force, tileIndex, range, attack_efficiency){
		var arroundEnemy = [];

		var payMap = tileEngine.MapPay(campaign.zone.direct_index, tileIndex, range);
		var troopVisibility = ForceInfo.GetTroopVisibility(forceInfo[force], campaign.troop, tileEngine);
		for (var troopid = 0; troopid < campaign.troop.length; troopid++){
			if (campaign.troop[troopid].strength <= 0)	continue;

			if (campaign.troop[troopid].force == force)
				continue;

			if (troopVisibility[troopid] == false)
				continue;
			
			if (campaign.troop[troopid].tileIndex < 0)
				continue;
			
			if (attack_efficiency[campaign.troop[troopid].troop_type] <= 0)
				continue;

			tilePay = payMap[campaign.troop[troopid].tileIndex].pay;
			if ( tilePay >= 0 && tilePay <= range )
				arroundEnemy[arroundEnemy.length] = troopid;
		}
		return arroundEnemy;
	},
};